# Contributor: Konstantin Kulikov <k.kulikov2@gmail.com>
# Maintainer: Adam Jensen <acjensen@gmail.com>
pkgname=bpftrace
pkgver=0.11.0
pkgrel=1
_llvmver=10
pkgdesc="High-level tracing language for Linux eBPF"
url="https://github.com/iovisor/bpftrace"
arch="aarch64 ppc64le x86_64"
license="Apache-2.0"
makedepends="cmake llvm$_llvmver-dev llvm$_llvmver-static clang-dev clang-static
	flex-dev bison elfutils-dev linux-headers bcc-dev binutils-dev"
source="$pkgname-$pkgver.tar.gz::https://github.com/iovisor/bpftrace/archive/v$pkgver.tar.gz"
# Tests require root, network to download gmock and a few tests fail.
options="!check"
subpackages="$pkgname-doc:doc $pkgname-tools:tools:noarch $pkgname-tools-doc:tools_doc"

build() {
	cmake -B build \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_CXX_FLAGS="$CXXFLAGS" \
		-DCMAKE_C_FLAGS="$CFLAGS" \
		-DBUILD_TESTING:BOOL=OFF \
		-DLLVM_REQUESTED_VERSION=$_llvmver
	make -C build
}

# Main package contains only bpftrace binary.
package() {
	make -C build DESTDIR="$pkgdir" install

	mv "$pkgdir/usr/share/bpftrace/tools/doc" "$pkgdir/usr/share/bpftrace/tools_doc"
}

# Doc package should contain only man page for bpftrace.
doc() {
	mkdir -p "$subpkgdir/usr/share/man/man8"
	mv "$pkgdir/usr/share/man/man8/bpftrace.8.gz" "$subpkgdir/usr/share/man/man8/"
}

# Tools are not installed in PATH, because they would conflict with other tools provided by bcc or perf-tools.
tools() {
	depends="$pkgname"
	pkgdesc="$pkgdesc (tools)"
	mkdir -p "$subpkgdir/usr/share/bpftrace"
	mv "$pkgdir/usr/share/bpftrace/tools" "$subpkgdir/usr/share/bpftrace/tools"
}

# Tools docs contains man pages and examples for tools. They shouldn't go into shared MANPATH for the same reasons as tools pkg above.
tools_doc() {
	pkgdesc="$pkgdesc (tool docs and examples)"

	mkdir -p "$subpkgdir/usr/share/bpftrace"
	mv "$pkgdir/usr/share/bpftrace/tools_doc" "$subpkgdir/usr/share/bpftrace/doc"
	mv "$pkgdir/usr/share/man/man8" "$subpkgdir/usr/share/bpftrace/"
}

sha512sums="32bf0c23a7b0e1a57d0e0b8fc845a9e184e201ac3f6018a3d3cee8c97096093b333578cb898ede02fb3ab8d55ba9bcd2bb67ac70b81a49461c0f6e5c03c2a6f5  bpftrace-0.11.0.tar.gz"
